import { createRouter, createWebHistory } from 'vue-router'

// 懒加载路由组件
const Layout = () => import('@/layout/Layout.vue')
const Login = () => import('@/views/login/Login.vue')
const Dashboard = () => import('@/views/dashboard/Dashboard.vue')
const UserList = () => import('@/views/user/UserList.vue')
const RoleList = () => import('@/views/role/RoleList.vue')
const PermissionList = () => import('@/views/permission/PermissionList.vue')
const AuditLogList = () => import('@/views/audit/AuditLogList.vue')

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '登录',
      requiresAuth: false
    }
  },
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    redirect: '/dashboard',
    meta: {
      requiresAuth: true
    },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: {
          title: '首页',
          icon: 'HomeFilled',
          requiresAuth: true
        }
      },
      {
        path: 'system',
        name: 'System',
        component: {
          render: (h) => h('router-view')
        },
        meta: {
          title: '系统管理',
          icon: 'Setting',
          requiresAuth: true
        },
        children: [
          {
            path: 'user',
            name: 'UserList',
            component: UserList,
            meta: {
              title: '用户管理',
              icon: 'User',
              requiresAuth: true,
              permission: 'sys:user:manage'
            }
          },
          {
            path: 'role',
            name: 'RoleList',
            component: RoleList,
            meta: {
              title: '角色管理',
              icon: 'Avatar',
              requiresAuth: true,
              permission: 'sys:role:manage'
            }
          },
          {
            path: 'permission',
            name: 'PermissionList',
            component: PermissionList,
            meta: {
              title: '权限管理',
              icon: 'Lock',
              requiresAuth: true,
              permission: 'sys:permission:manage'
            }
          },
          {
            path: 'audit',
            name: 'AuditLogList',
            component: AuditLogList,
            meta: {
              title: '审计日志',
              icon: 'Document',
              requiresAuth: true,
              permission: 'sys:audit:view'
            }
          }
        ]
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/error/404.vue'),
    meta: {
      title: '页面不存在',
      requiresAuth: false
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = `${to.meta.title || '管理后台'}`

  // 判断是否需要登录
  if (to.meta.requiresAuth) {
    const token = localStorage.getItem('token')
    if (!token) {
      next({ name: 'Login', query: { redirect: to.fullPath } })
      return
    }

    // 判断是否有权限访问
    if (to.meta.permission) {
      const permissions = JSON.parse(localStorage.getItem('permissions') || '[]')
      if (!permissions.includes(to.meta.permission)) {
        // 如果没有权限，显示403页面
        next({ name: 'NotFound' })
        return
      }
    }
  }

  next()
})

export default router